<template>
  <div class="log-box-style">
    <div class="query-form-style">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="短信状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择" clearable>
            <el-option
              v-for="item in msg_send_status"
              :key="item.code"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作时间">
          <el-date-picker
            v-model="dateRange"
            style="width: 360px"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetimerange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="handleResetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table ref="adminTable" :data="resultList" style="width: 100%;" border>
      <el-table-column label="手机号" prop="phone" align="center" min-width="100"></el-table-column>
      <el-table-column label="验证码" prop="code" align="center" min-width="100"></el-table-column>
      <el-table-column label="短信序列号" prop="serialNo" align="center" min-width="100"></el-table-column>
      <el-table-column label="上下文对象" prop="sessionCode" align="center" min-width="100"></el-table-column>
      <el-table-column label="数据类型" prop="sourceType" align="center" min-width="100"></el-table-column>
      <el-table-column label="状态" prop="status" align="center" min-width="100">
        <template slot-scope="scope">
          <div v-for="(item, index) in msg_send_status" :key="index"
               v-show="item.code == scope.row.status">{{item.name}}</div>
        </template>
      </el-table-column>
      <el-table-column label="获取时间" width="160" align="center">
        <template slot-scope="scope">{{scope.row.createTime | formatDateTime}}</template>
      </el-table-column>
      <el-table-column label="过期时间" width="160" align="center">
        <template slot-scope="scope">{{scope.row.expiredMinute | formatDateTime}}</template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="queryParams.pageNum"
        :page-size="queryParams.pageSize"
        :page-sizes="[10,15,20, 50, 100]"
        :total="resultTotal">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {formatDate} from '@/utils/date';

const defaultListQuery = {
  pageNum: 1,
  pageSize: 10,
  status: null,
  phone: null,
};

export default {
  name: "index",
  data() {
    return {
      queryParams: Object.assign({}, defaultListQuery),
      resultList: [],
      resultTotal: 0,
      // 日期范围
      dateRange: [],
      msg_send_status: []
    }
  },
  filters: {
    formatDateTime(time) {
      if (time == null || time === '') {
        return 'N/A';
      }
      let date = new Date(time);
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    }
  },
  created() {
    this.handleQuery();
    this.getDict();
  },
  methods: {
    getDict() {
      this.$commonList("/dict/getListByType", "GET", { codeType: "msg_send_status" }).then(res => {
        if (res.code === 200) {
          this.msg_send_status = res.data;
        }
      })
    },
    /** 搜索 + 分页 */
    handleResetSearch() {
      this.dateRange = [];
      this.queryParams = Object.assign({}, defaultListQuery);
      this.handleQuery();
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getList() {
      this.resultList = [];
      let dateRange = Array.isArray(this.dateRange) ? this.dateRange : [];
      this.queryParams.startTime = dateRange[0];
      this.queryParams.endTime = dateRange[1];
      this.$commonList("/messageHistory/list", "GET", this.queryParams).then(res => {
        if (res.code === 200) {
          this.resultList = res.data.list;
          this.resultTotal = res.data.total;
        }
      })
    },
    handleSizeChange(val) {
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getList();
    },
  },
}
</script>

<style lang="scss" scoped>
.log-box-style {
  color: red;
  padding: 20px;
  .query-form-style {

  }
}
</style>
